package qqtestprotocol

import (
	"encoding/json"
	"net"

	"gitee.com/viplianghl/golib/liangtcp"
)

type Protocol_cmd struct {
	Cmd string `json:"cmd"`
}

type Protocol_error struct {
	Cmd   string `json:"cmd"` // "error"
	Error string `json:"error"`
}

type Protocol_success struct {
	Cmd     string  `json:"cmd"` // "success"
	Uid     int64   `json:"uid,string"`
	Nick    string  `json:"nick"`
	Friends []int64 `json:"friends"`
}

type Protocol_regist struct {
	Cmd  string `json:"cmd"` // "regist"
	Nick string `json:"nick"`
	Pwd  string `json:"pwd"`
}

type Protocol_login struct {
	Cmd string `json:"cmd"` // "login"
	Uid int64  `json:"uid,string"`
	Pwd string `json:"pwd"`
}

type Protocol_add_friend struct {
	Cmd string `json:"cmd"` // "add"
	Uid int64  `json:"uid,string"`
	Fid int64  `json:"fid,string"`
}

type Protocol_del_friend struct {
	Cmd string `json:"cmd"` // "del"
	Uid int64  `json:"uid,string"`
	Fid int64  `json:"fid,string"`
}

type Protocol_msg struct {
	Cmd string `json:"cmd"` // "send"
	Uid int64  `json:"uid,string"`
	Fid int64  `json:"fid,string"`
	Msg string `json:"msg"`
}

func TransProtocol(buf []byte, v interface{}) error {
	return json.Unmarshal(buf, v)
}

func SendMsg(t liangtcp.LiangTCP, conn net.Conn, data Protocol_msg) error {

	jsons, errs := json.Marshal(data)
	if errs != nil {
		return errs
	}

	return t.SendMsg(conn, jsons)
}

func SendErrMsg(t liangtcp.LiangTCP, conn net.Conn, err string) error {
	cmd := Protocol_error{Cmd: "error", Error: err}
	jsons, errs := json.Marshal(cmd)
	if errs != nil {
		return errs
	}

	return t.SendMsg(conn, jsons)
}

func SendSuccessMsg(t liangtcp.LiangTCP, conn net.Conn, data Protocol_success) error {

	jsons, errs := json.Marshal(data)
	if errs != nil {
		return errs
	}

	return t.SendMsg(conn, jsons)
}
